package com.lyk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_adminer;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lyk.service.A_adminerService;
import com.lyk.service.impl.A_adminerServiceImpl;

public class A_adminerServlet extends HttpServlet {

	@Override
	public void init() throws ServletException {
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		String opr=request.getParameter("opr");
		if("del".equals(opr)){
			A_adminerService adminerService = new A_adminerServiceImpl();
			int num = 0;
			String errMsg = "";
			try{
				Integer n = StringUtils.str2Integer(request.getParameter("admin_id"),0);
				if(n != null){
					num = adminerService.delA_adminer(n);
				}else{
					throw new RuntimeException("管理员编号必须输入");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(num>0){
				out.println("<script type='text/javascript'>alert('删除成功');location.href='admin_list.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('删除失败：:需要删除管理员的回复投诉信息"+errMsg+"');history.back();</script>");
			}
		}else if("updatepwd".equals(opr)){
			PrintWriter out=resp.getWriter();
			int num = 0;
			String errMsg = "";
			try{	
				A_adminerService adminerService = new A_adminerServiceImpl();
				Integer n = ((A_adminer)request.getSession().getAttribute("userInfo")).getAdmin_id();
				if(n != null){
					A_adminer adminer = adminerService.findById(n);
					String admin_pwd = adminer.getAdmin_pwd();
					if(request.getParameter("oldpwd").equals(admin_pwd)){
						adminer.setAdmin_pwd(request.getParameter("secondpwd"));
						adminer.setAdmin_id(n);
						num = adminerService.changeEmp(adminer);
					}else{
						out.println("<script type='text/javascript'>alert('原密码输入错误');location.href='admin_info.jsp';</script>");
					}
				}else{
					throw new RuntimeException("管理员编号必须输入");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			if(num>0){
				out.println("<script type='text/javascript'>alert('修改成功');location.href='admin_info.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('修改失败');location.href='admin_info.jsp';</script>");
			}
		}else if("update".equals(opr)){
			PrintWriter out=resp.getWriter();
			int num = 0;
			String errMsg = "";
			try{	
				A_adminerService adminerService = new A_adminerServiceImpl();
				Integer n = ((A_adminer)request.getSession().getAttribute("userInfo")).getAdmin_id();
				if(n != null){
					A_adminer adminer = adminerService.findById(n);
					adminer.setAdmin_name(request.getParameter("admin_name"));
					adminer.setTelephone(request.getParameter("telephone"));
					adminer.setRealName(request.getParameter("realName"));
					adminer.setAdmin_id(n);
					num = adminerService.changeEmp(adminer);
					//清除登录的userInfo
					//request.getSession().removeAttribute("userInfo");
					//重置登录的userInfo
					request.getSession().setAttribute("userInfo",adminer);	
				}else{
					throw new RuntimeException("管理员编号必须输入");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			if(num>0){
				out.println("<script type='text/javascript'>alert('修改成功');location.href='admin_info.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('修改失败');location.href='admin_info.jsp';</script>");
			}
		}else if("add".equals(opr)){
			A_adminer adminer = new A_adminer();
			int rows=0;
			String errMsg="";
			try{
				adminer.setAdmin_name(request.getParameter("admin_name"));
				adminer.setAdmin_pwd(request.getParameter("admin_pwd"));
				adminer.setRealName(request.getParameter("realName"));
				adminer.setTelephone(request.getParameter("telephone"));
				A_adminerService adminerService = new A_adminerServiceImpl();
				rows=adminerService.saveEmp(adminer);
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(rows>0){
				out.println("<script type='text/javascript'>alert('新增成功');location.href='admin_list.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('新增失败"+errMsg+"');history.back();</script>");
			}
		}else{
			list(request,resp);
		}
	}
	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		Map<String, Object> map=new HashMap<String, Object>();
		String adminer_name = request.getParameter("adminer_name");
		if(StringUtils.isNotNull(adminer_name)){
			map.put("adminer_name","%" + adminer_name + "%");
		}
		String date = request.getParameter("adminer_time");
		if(StringUtils.isNotNull(date)){
			map.put("adminer_time",date);
		}
		A_adminerService adminerService = new A_adminerServiceImpl();
		Integer pageIndex=StringUtils.str2Integer(request.getParameter("pageIndex"), 1);
		Integer pageSize = StringUtils.str2Integer(request.getParameter("pageSize"), 3);
		PageBean<A_adminer> pageBean = adminerService.findPageByCondition(pageIndex, pageSize, map);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(pageBean);
		PrintWriter out=resp.getWriter();
		out.print(json);
	}
}
